package com.blb.goods_service.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.goods_service.entity.JsonResult;
import com.blb.goods_service.entity.Specification;
import com.blb.goods_service.service.ISpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/spec")
public class SpecificationController {

    @Autowired
    private ISpecificationService specificationService;

    @GetMapping("/category/{cid}")
    private JsonResult findSpecByCategory(@PathVariable("cid")Integer cid){
        try {
            Specification spec = specificationService.getOne(new QueryWrapper<Specification>().lambda()
                    .eq(Specification::getCategoryId, cid));
            return new JsonResult(1,spec);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0, null);
        }
    }

    @PostMapping
    public JsonResult saveSpec(@RequestBody Specification specification){
        try {
            specificationService.save(specification);
            return new JsonResult(1,null);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @PutMapping
    public JsonResult updateSpec(@RequestBody Specification specification){
        try {
            specificationService.updateById(specification);
            return new JsonResult(1,null);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }
}
